<?php
// +----------------------------------------------------------------------
// | User: zq
// +----------------------------------------------------------------------
// | Time: 2021/12/3 15:53
// +----------------------------------------------------------------------

namespace App\Services;

use App\CodeResponse;
use App\Exceptions\BusinessException;

class BaseService
{
    // 单例对象
    protected static $instance;

    /**
     * 单例方法
     * @return static
     */
    public static function getInstance()
    {
        if (static::$instance instanceof static) {
            return static::$instance;
        }
        static::$instance = new static();
        return static::$instance;
    }

    // 使用单例, 重置构造
    protected function __construct()
    {
    }

    // 使用单例, 重置克隆
    protected function __clone()
    {
    }

    // 抛出错误
    public function throwBusinessException(array $codeResponse, $info = '')
    {
        throw new BusinessException($codeResponse, $info);
    }

    /**
     * 抛出402错误
     * @throws BusinessException
     */
    public function throwBadArgumentValue()
    {
        $this->throwBusinessException(CodeResponse::PARAM_VALUE_ILLEGAL);
    }

    /**
     * 抛出505错误, 更新数据失败
     * @throws BusinessException
     */
    public function throwUpdateFail()
    {
        $this->throwBusinessException(CodeResponse::UPDATED_FAIL);
    }
}